namespace ASPNET.StarterKit.IssueTracker.BusinessLogicLayer
{
    using ASPNET.StarterKit.IssueTracker.DataAccessLayer;
    using System;

    public class IssueComment
    {
        private string _Comment;
        private string _CreatorDisplayName;
        private string _CreatorUsername;
        private DateTime _DateCreated;
        private int _Id;
        private int _IssueId;

        public IssueComment(int issueId, string comment, string creatorUsername) : this(DefaultValues.GetIssueCommentIdMinValue(), issueId, comment, creatorUsername, string.Empty, DefaultValues.GetDateTimeMinValue())
        {
        }

        public IssueComment(int commentId, int issueId, string comment, string creatorUsername, string creatorDisplayName, DateTime created)
        {
            if ((comment == null) || (comment.Length == 0))
            {
                throw new ArgumentOutOfRangeException("comment");
            }
            if (issueId <= DefaultValues.GetIssueIdMinValue())
            {
                throw new ArgumentOutOfRangeException("IssueId");
            }
            this._Id = commentId;
            this._IssueId = issueId;
            this._CreatorUsername = creatorUsername;
            this._CreatorDisplayName = creatorDisplayName;
            this._Comment = comment;
            this._DateCreated = created;
        }

        public static IssueComment CreateNewIssueComment(int issueId, string comment, string userName)
        {
            IssueComment str = new IssueComment(issueId, comment, userName);
            if (str.Save())
            {
                return str;
            }
            return null;
        }

        public static IssueCommentCollection GetIssueCommentsByIssueId(int issueId)
        {
            if (issueId <= DefaultValues.GetIssueIdMinValue())
            {
                throw new ArgumentOutOfRangeException("issueId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetIssueCommentsByIssueId(issueId);
        }

        public bool Save()
        {
            int TempId = DataAccessLayerBaseClassHelper.GetDataAccessLayer().CreateNewIssueComment(this);
            if (TempId > 0)
            {
                this._Id = TempId;
                IssueNotification.SendIssueNotifications(this.IssueId);
                return true;
            }
            return false;
        }

        public string Comment
        {
            get
            {
                if ((this._Comment == null) || (this._Comment.Length == 0))
                {
                    return string.Empty;
                }
                return this._Comment;
            }
            set
            {
                this._Comment = value;
            }
        }

        public string CreatorDisplayName
        {
            get
            {
                if ((this._CreatorDisplayName == null) || (this._CreatorDisplayName.Length == 0))
                {
                    return string.Empty;
                }
                return this._CreatorDisplayName;
            }
        }

        public string CreatorUsername
        {
            get
            {
                if ((this._CreatorUsername == null) || (this._CreatorUsername.Length == 0))
                {
                    return string.Empty;
                }
                return this._CreatorUsername;
            }
        }

        public DateTime DateCreated
        {
            get
            {
                return this._DateCreated;
            }
        }

        public int Id
        {
            get
            {
                return this._Id;
            }
        }

        public int IssueId
        {
            get
            {
                return this._IssueId;
            }
            set
            {
                if (value <= DefaultValues.GetIssueIdMinValue())
                {
                    throw new ArgumentOutOfRangeException("value");
                }
                this._IssueId = value;
            }
        }
    }
}

